<template>
  <div class="app-container">
    <div class="main">
      <div class="guidance-main">
        <div class="patient-baseInfo">
          <div class="baseInfo-item">姓名： {{patient.name}}</div>
          <div class="baseInfo-item">性别： {{patient.sex}}</div>
          <div class="baseInfo-item">年龄： {{patient.age}}</div>
        </div>
        <el-divider ></el-divider>
        <div class="guidanceInfo">
          <el-form ref="form" :model="form" label-width="120px" :rules="formRules" class="guidance-form">
            <el-row :gutter= 8>
              <el-col :span=12>
                <el-form-item label="就诊类型" prop="type">
                  <el-select v-model="form.type" placeholder="请选择" >
                    <el-option
                      v-for="item in typeEnum"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span=12>
                <el-form-item label="主治医生" prop="doctor">
                  <el-input v-model="patient.masterDoctorName" :disabled="true"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter= 8>
              <el-col :span=12 :offset="12">
                <el-form-item label="就诊医生" prop="doctor">
                  <el-select v-model="form.doctorId" placeholder="请选择" >
                    <el-option
                      v-for="item in userEnum"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                      @click.native="upDoctor(item)"
                      >
                    </el-option>
                  </el-select>
                  <span style="margin-left: 10px">挂号费：{{registerPrice}}元</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter= 8>
              <el-col :span=12 :offset="12">
                <el-form-item label="就诊地点" prop="address">
                  <el-select v-model="form.workAddressId" placeholder="请选择" :disabled="true">
                    <el-option
                      v-for="item in workAddressEnum"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row> <el-row :gutter= 8>
              <el-col :span=12 :offset="12">
                <el-form-item label="科室" prop="jobOffice">
                  <el-select v-model="form.jobOfficeId" placeholder="请选择" >
                    <el-option
                      v-for="item in jobOfficeEnum"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                      >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter= 8>
              <el-col :span=12 :offset="12">
                <el-form-item label="候诊时间" prop="waitTime">
                  <el-autocomplete
                    value-key="waitTime"
                    class="inline-input"
                    v-model="form.waitTime"
                    :fetch-suggestions="querySearch"
                    placeholder="请输入候诊时间"
                    @select="handleSelect"
                    style="width: 100%;"
                  >
                  <template slot-scope="{ item }">
                    <span >{{ item}}</span>
                  </template>
                  <template slot="append">分钟</template>
                  </el-autocomplete>
                </el-form-item>
              </el-col>
            </el-row>
            <div class="footer">
              <el-button type="primary" @click="confirm">确 定</el-button>
              <div style="width:200px;display: inline-block;"> </div>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </el-form>
        </div>
      </div>
      <div class="patient-info">
        <h2>会员信息</h2>
        <div>
          <p>姓名: {{ this.patient.name }}</p>
          <p>性别: {{ this.patient.sex }}</p>
          <p>年龄: {{ this.patient.age }}</p>
          <p>主治医生: {{ this.patient.masterDoctorName }}</p>
          <p>就诊次数: 2次</p>
          <p>会员等级: 1级</p>
          <p>最近就诊时间: 2024-04-25 10:15:22</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

import { getList as getUserList, getListByDept as getUserListByDept, getLoginInfo} from '@/api/user'
import { getListByDeptId as getWorkAddressListByDeptId } from '@/api/workAddress'
import { getList as getJobOfficeList} from '@/api/jobOffice'
import { addOrUpdate} from '@/api/guidance'

export default {
  filters: {

  },
  data() {
    const validateWaitTime = (rule, value, callback) => {
      if (!value) callback(new Error('请输入候诊时间')) 
      else callback()
    }
    return {
      list: null,
      listLoading: true,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      curIndex: -1,
      infoVisible: false,
      formVisible: false,
      info: {},
      form: {},
      dialogTitle: '添加来源',
      depts:[],
      formRules: {
        waitTime: [{ required: true, trigger: 'blur', validator: validateWaitTime }]
      },
      typeEnum: [{id: 1, name: '店面就诊'},{id: 2, name: '邮寄'},{id: 3, name: '代取'},{id: 4, name: '代放'}],
      waitTimeEnum: ['5', '10', '15', '20', '30', '60', '120'],
      jobOfficeEnum: [],
      jobOfficeEnumAll: [],
      userEnum: [],
      workAddressEnum: [],
      registerPrice: 0,
      patient: {},
      loginDept: {},
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.patient = this.$route.params
      this.form.patientId = this.patient.id
      this.$set(this.form, 'registerPrice', this.registerPrice)
      this.$set(this.form, 'waitTime', "10")
      this.loginDept = this.$store.getters.userInfo.loginInfo.loginDept
      getUserListByDept(this.loginDept.deptId).then(res=>{
        console.log("res", res)
        this.userEnum = res.data
      })
      getJobOfficeList().then(res=>{
        this.jobOfficeEnumAll = res.data
      })

      this.form.type = 1  // 默认店面就诊
      this.form.doctorId = this.patient.masterDoctorName
    },
    clickCurPage(val){
      this.currentPage = val
      this.fetchData()
    },
    getInfo(row) {
      this.info = row
      this.infoVisible = true
    },
    update(row) {
      this.reset()
      this.dialogTitle = '修改来源'
      this.form = JSON.parse(JSON.stringify(row))
      this.formVisible = true
    },
    deleteInfo(scope) {
      console.log(scope.row)
      this.$confirm('确定删除该来源?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(scope.row.id).then(res=>{
          this.list.splice(scope.$index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(err=>{
          console.log(err)
        })
        
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    add(row) {
      this.reset()
      this.dialogTitle = '添加来源'
      this.formVisible = true
    },
    confirm() {
      this.form.deptId = this.loginDept.deptId
      this.$refs.form.validate(valid => {
        if (valid) {
          console.log(this.form)
          addOrUpdate(this.form).then(res=>{
            console.log(res)
            this.$message({
              message: '添加/修改成功',
              type: 'success'
            })
            this.formVisible = false
            this.$router.push('/guidance/guidanceList')
          }).catch(err=>{
            console.log(err)
          })
        }
      })
    },
    cancel() {
      this.$router.push('/guidance/patient');
    },
    reset(){
      this.form = {}
    },
    querySearch(queryString, cb) {
      // 调用 callback 返回建议列表的数据
      cb(this.waitTimeEnum);
    },
    handleSelect(item) {
      this.$set(this.form, 'waitTime', item)
    },
    upDoctor(value) {
      console.log(value)
      this.registerPrice = value.registerPrice
      getLoginInfo(value.id).then(res=> {
        var loginInfo = res.data.userDeptWorkAddressJobOffice
        this.jobOfficeEnum = this.getJobOfficeEnum(loginInfo.jobOfficeId)
        this.$set(this.form, 'jobOfficeId', loginInfo.jobOfficeId)
        this.form.workAddressId = loginInfo.workAddressId
        this.workAddressEnum = [{id: loginInfo.workAddressId, name: loginInfo.workAddressName}]
      })
      // getWorkAddressListByDeptId({deptId: this.loginDept.deptId}).then(res=>{
      //   this.workAddressEnum = res.data
      // })
      
    },
    getJobOfficeEnum(jobOfficeId) {
      return this.jobOfficeEnumAll.filter(jobOffice => jobOffice.id == jobOfficeId)
    }
  }
}
</script>

<style scoped>

.main{
  display: flex;
  flex-direction: row;
}
.patient-info {
  position: absolute;
  right: 0px;
  display: flex;
  flex-direction: column;
  background-color: white;
  margin-top: -20px;
  padding: 20px;
  height: 1000px;
  width: 20%;
  overflow: auto;
  border: 1px solid gray;
}
.guidance-main {
  display: flex;
  flex-direction: column;
  width: 75%;
}
.patient-baseInfo {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
}
.guidance-form {
  margin: 20px;
  width: 80%;
}
.footer {
    margin-top: 100px;
    margin-left: 400px;
}
</style>